<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <button onclick="fn()">点击</button>
        <button class="btn">点击2</button>
    </head>
    <body>
        <script>
            function fn() {
                console.log(this, 666);
            }

            document.querySelector('.btn').onclick = fn;
        </script>
        <script>
            let arr = [
                { id: 0, pid: '', name: 'aaaa' },
                { id: 1, pid: 0, name: 'bbbb' },
                { id: 2, pid: 1, name: 'cccc' },
                { id: 3, pid: 1, name: 'dddd' },
            ];

            function toTree(arr, id) {
                let res = [];
                arr.forEach((item) => {
                    if (item.pid === id) {
                        res.push(item);
                        item.children = toTree(arr, item.id);
                    }
                });
                return res;
            }

            let res = toTree(arr, '');
            console.log(res);
        </script>
    </body>
</html>
